package model;

import java.util.ArrayList;
import java.util.List;

import javax.swing.table.AbstractTableModel;

import domain.Copy;

public class NewLoansOfPersonTableModel extends AbstractTableModel {
    private String[] columns = {"ID", "Titel", "Autor", "Verlag" };
    private List<Copy> model;
    private static final long serialVersionUID = 2966279676059941187L;

    public static NewLoansOfPersonTableModel getInstance() {
        return new NewLoansOfPersonTableModel();
    }

    private NewLoansOfPersonTableModel() {
        model = new ArrayList<>();
    }

    public void addCopy(Copy copy) {
        this.model.add(copy);
        fireTableDataChanged();
    }

    public void removeCopy(Copy copy) {
        this.model.remove(copy);
        fireTableDataChanged();
    }

    public boolean containsCopy(Copy copy) {
        return this.model.contains(copy);
    }

    public List<Copy> getCopies() {
        return model;
    }

    @Override
    public boolean isCellEditable(int arg0, int arg1) {
        return false;
    }

    @Override
    public String getColumnName(int column) {
        return columns[column];
    }

    @Override
    public int getColumnCount() {
        return columns.length;
    }

    @Override
    public int getRowCount() {
        return model.size();
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        Copy copy;
        copy = model.get(rowIndex);
        switch (columnIndex) {
            case 0:
                return copy.getInventoryNumber();
            case 1:
                return copy.getTitle().getName();
            case 2:
                return copy.getTitle().getAuthor();
            case 3:
                return copy.getTitle().getPublisher();
            default:
                return null;
        }
    }

    public Copy getElementAt(int rowIndex) {
        try {
            return model.get(rowIndex);
        } catch (ArrayIndexOutOfBoundsException ex) {
            return null;
        }
    }

    @Override
    public Class<?> getColumnClass(int columnIndex) {
        switch (columnIndex) {
            case 0:
                return Integer.class;
            case 1:
            case 2:
            case 3:
                return String.class;
            default:
                return null;
        }
    }

}
